package com.blockmeta.bbs.immodule.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.bean.AddNoticeRequest;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.corebean.IMMetaBase;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.eventbus.UserInfoChangeEvent;
import com.blockmeta.bbs.immodule.net.api.AddNotice;
import com.blockmeta.bbs.immodule.net.api.GetNewNotice;
import com.blockmeta.bbs.immodule.net.api.GetRemarkNameApi;
import com.blockmeta.bbs.immodule.net.api.RemarkFriendApi;
import com.blockmeta.bbs.immodule.net.api.RemarkMyNameInGroup;
import com.blockmeta.bbs.immodule.net.pojo.Notice;
import com.blockmeta.bbs.immodule.widget.IMAddFriendMsg;
import com.blockmeta.bbs.immodule.widget.IMConfirmPop;
import com.blockmeta.bbs.immodule.widget.LengthLimitEditText;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.blockmeta.bbs.immodule.bean.Params.REMARK;

/**
 * @author airthor
 * @date 2019/2/21
 * 备注好友
 */
public class CircleNoticeActivity extends BaseActivity {

    @BindView(R2.id.edt_circle_notice)
    LengthLimitEditText mEdtCircleNotice;
    @BindView(R2.id.btn_save)
    Button mBtnSave;
    @BindView(R2.id.tv_notice_content)
    TextView mTvNoticeContent;

    private long mCircleId;
    private boolean mIsCircleCreater;
//    private String mCircleNotice = "";

    @Override
    protected boolean showTitle() {
        return false;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_write_circle_notice, null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mCircleId = getIntent().getLongExtra(Params.CIRCLE_ID, 0);
        mIsCircleCreater = getIntent().getBooleanExtra(Params.IS_CIRCLE_CREATER, false);
//        mCircleNotice = getIntent().getStringExtra(Params.CIRCLENOTICE);


        if(!mIsCircleCreater){
            mEdtCircleNotice.setVisibility(View.GONE);
            mTvNoticeContent.setVisibility(View.VISIBLE);
            mBtnSave.setVisibility(View.GONE);
        }else {
            mEdtCircleNotice.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if(s.toString().length() > 0){
                        mBtnSave.setEnabled(true);
                    }else {
                        mBtnSave.setEnabled(false);
                    }
                }
            });
        }
        mBtnSave.setEnabled(false);

        getCircleNotice();
    }

    private void getCircleNotice() {

        ProgressBarUtil.showProgress(mContext);

        RetrofitFactory.getRootRetrofit().create(GetNewNotice.class)
                .getNewNotice(mCircleId)
                .compose(this.<BaseImResponse<Notice>>bindToLifecycle())
                .compose(RxUtil.<BaseImResponse<Notice>>rxSchedulerHelper())
                .compose(RxUtil.<Notice>dataNullResult())
                .subscribe(new CommonSubscriber<BaseImResponse<Notice>>(this) {

                    @Override
                    public void onNext(BaseImResponse<Notice> result) {
                        if (result == null || result.data == null) {
                            return;
                        }
                        setCircleNotice(result.data);
                    }
                });
    }

    private void setCircleNotice(Notice notice) {
        if (notice != null) {
            try {
                String content = notice.getContent();
                int sourceLength = content.getBytes("GBK").length/2;
                if(sourceLength > 300){
                    //为了防止有些手机可以输入超长导致一直转先截取一下,同时把读写控件分开
                    content = content.substring(0, 300);
                }
                mEdtCircleNotice.setText(content);
                mEdtCircleNotice.setSelection(content.length());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            mTvNoticeContent.setText(notice.getContent());
        }
    }


    @OnClick({R2.id.fl_back, R2.id.fl_edit, R2.id.btn_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.fl_back) {
            finish();

        } else if (i == R.id.btn_save) {

            writeCircleNotice();
        } else {
        }
    }

    private void writeCircleNotice() {
        ProgressBarUtil.showProgress(mContext);

        IMConfirmPop.OnRequestSuccessListiener requestSuccessListiener = new IMConfirmPop.OnRequestSuccessListiener() {
            @Override
            public void requestSuccess() {

                final String circleNotice = mEdtCircleNotice.getText().toString().trim();
                AddNoticeRequest addNoticeRequest = new AddNoticeRequest();
                addNoticeRequest.setChatGroupId(mCircleId);
                addNoticeRequest.setContent(circleNotice);
                RetrofitFactory.getRootRetrofit().create(AddNotice.class)
                        .addNotice(addNoticeRequest)
                        .compose(RxUtil.dataNullResult())
                        .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                        .compose(((RxAppCompatActivity) mContext).<BaseImResponse<Object>>bindToLifecycle())
                        .subscribe(new CommonSubscriber<BaseImResponse<Object>>(mContext) {
                            @Override
                            public void onNext(BaseImResponse<Object> o) {
                                ToastUtil.show("已发布");
                                IMApplicationContext.getIMMetaService().post(new IMMetaBase.Event_AddCircleNotice(mCircleId));
                                finish();
                            }
                        });
            }
        };
        IMConfirmPop imConfirmPop = new IMConfirmPop(mContext, requestSuccessListiener);
        imConfirmPop.show();

    }


}
